package com.wanho.java156.service;

import com.wanho.java156.mapper.MemberMapper;
import com.wanho.java156.po.Member;
import com.wanho.java156.po.MemberExample;
import com.wanho.java156.util.CodeUtil;
import com.wanho.java156.redisutil.RedisUtil;
import com.wanho.java156.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class MemberService {
    @Autowired
    private MemberMapper memberMapper ;
    @Autowired
    private SmsUtil smsUtil ;
    @Autowired
    private RedisUtil redisUtil ;
    @Autowired
    private CodeUtil codeUtil ;

    public String sendSmsCode(String phone){
        //1. 生成短信 4 位随机数字验证码
        String randCode = codeUtil.randCode();
        //2. 调用 短信发送功能 接口 [测试阶段 暂时不发送短信  只存redis ]
        String responseCode = smsUtil.send(phone, randCode);
        if ("1".equals(responseCode)) {
            //3. 短信 存放在 redis 5 分钟失效
            redisUtil.set("sms:code:"+phone,randCode,5, TimeUnit.MINUTES);
        }
        return responseCode ;
    }

    public List<Member> selectAll(){
        return memberMapper.selectByExample(new MemberExample()) ;
    }

    public boolean save(Member member){
        return memberMapper.insert(member)>0 ;
    }

}
